import { defineStore } from "pinia";
import { ElMessage } from "element-plus";
import { ref, reactive } from "vue"
import { useEnumStore } from "@/store/enumeration"
import { schemePageGet, equityDetailGet, schemeDetailGet, schemeDeleteDel } from "@/api/equity"
import useInstance from '@/hooks/useInstance';
import mittBus from '@/utils/mitt';

export const schemeListStore = defineStore("schemeListStoreId1", () => {
    const { global } = useInstance();
    const detailRef = ref<{ show: (title: string, row?: any) => void }>()
    const schemeRef = ref<{ show: (type: string, row?: any) => void }>()
    const loopRef = ref<{ show: (type: string, row?: any) => void }>()


    const { expiryPeriodType, reductionVoucherType, schemeType } = useEnumStore()
    const detailData = ref([
        {
            prop: "supplierCode",
            name: "供应商编码",
            type: "text",
            value: null
        },
   
    ])
    const tableProp = reactive(
        {
            name: "supplierList",
            propsList: [

                {
                    prop: 'schemeCode',
                    label: '方案编码',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'maxAnnualPayNum',
                    label: '最大续期次数',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'schemeName',
                    label: '方案名称',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'schemeType',
                    label: '方案类型',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },

                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '200px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearch = reactive({
        condition: {
            schemeCode: null, //方案编码
            schemeName: null, //方案名称
            schemeType: null, //方案类型
        },
        orders: ["created_time desc"],
        pageNum: 20,
        pageSize: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []
    })

    const getDate1 = async (type: string) => {
        pageParams.loadIng = true
    
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
            // @ts-ignore
                listSearch.condition[it] = null
            })
        }
        try {
            const res = await schemePageGet(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        schemeType: schemeType[item.schemeType - 1]?.value
                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false

        }finally{
            mittBus.emit('scrollUpdate')
        }
    }
    const detailBtn = async (row: any) => {
        try {
            const res = await schemeDetailGet({ id: row.id })
            loopRef.value?.show('edit', res.data)
        } catch (error) {

        }
    }
    const quotaBtn = async (row: any) => {
        // quotaRef.value?.show('add', row)

    }
    const handPage = (params: any) => {
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }
    const delBtn = async (row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.schemeName}`)
        if (flag) {
            const res = await schemeDeleteDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getDate1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    const AddEdit = async (type: string, row?: any) => {
        if (type == 'add') {
            schemeRef.value?.show(type)
        } else {
            const res = await schemeDetailGet({ id: row.id })
            schemeRef.value?.show(type, res.data)

        }
    }
    return {
        detailData, tableProp, listSearch, pageParams, detailRef, schemeRef, loopRef,
        getDate1, handPage, delBtn, detailBtn, quotaBtn, AddEdit
    }
})